package cj.web.lkg.dao;

import java.util.List;
import java.util.Map;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.SQLParam;

@DAO
public interface AppUserDAO {
	//列表
	@SQL("SELECT id,devicesId,photo,account,name,phone FROM AppUserInfo WHERE id=:1 ")
    List<Map> queryAppUserById(int id);
	
	//列表
	@SQL("SELECT id,devicesId,uuid,photo,name FROM AppDevicesAndUid WHERE uuid=:1 ")
    List<Map> queryAppDevicesAndUid(String uuid);
	//列表
	@SQL("SELECT id,devicesId,photo,account,name,phone,state,weixinuuid,qquuid,weibouuid,wxid,qqid,sinawbid,accountlogin FROM AppUserInfo WHERE devicesId=:1 ")
    List<Map> queryAppUserByDevicesId(String devicesId);
	//列表wxid
	@SQL("SELECT id,devicesId,photo,account,name,phone,state,weixinuuid,qquuid,weibouuid,wxid,qqid,sinawbid,accountlogin,account FROM AppUserInfo WHERE wxid=:1 ")
    List<Map> queryAppUserByWXid(String wxid);
	//列表qqid
	@SQL("SELECT id,devicesId,photo,account,name,phone,state,weixinuuid,qquuid,weibouuid,wxid,qqid,sinawbid,accountlogin,account FROM AppUserInfo WHERE qqid=:1 ")
    List<Map> queryAppUserByQQid(String qqid);
	//列表sinawbid
	@SQL("SELECT id,devicesId,photo,account,name,phone,state,weixinuuid,qquuid,weibouuid,wxid,qqid,sinawbid,accountlogin,account FROM AppUserInfo WHERE sinawbid=:1 ")
    List<Map> queryAppUserBySinawbid(String sinawbid);
	//初始化新建设备用户
	@SQL("INSERT INTO AppUserInfo (devicesId,type,state,deleteFlag,createUser,createAt,updateUser,updateAt,weixinuuid,qquuid,weibouuid,accountlogin,appclient) VALUES (:t.devicesId,1,:t.state,:t.deleteFlag,:t.createUser,now(),:t.updateUser,now(),0,0,0,0,:t.appclient)")
	void create(@SQLParam("t") Map rep);
	
	@SQL("UPDATE AppUserInfo SET appclient=:2 WHERE devicesId=:1")
	void updateAppUserInfo(String devicesId,String appclient);
	//第三方登录
	@SQL("UPDATE AppUserInfo SET weixinuuid=:2,photo=:3,name=:4,qquuid=:5,weibouuid=:6,accountlogin=:10 #if(:7!=''){ ,wxid=:7 } #if(:8!=''){ ,qqid=:8 } #if(:9!=''){ ,sinawbid=:9 } #if(:11!=''){ ,account=:11 } WHERE devicesId=:1")
    void upLoginByDevicesId(String devicesId,String weixinuuid,String photo,String name,String qquuid,String weibouuid,String wxid,String qqid,String sinawbid,String accountlogin,String account);
	//第三方登出
	@SQL("UPDATE AppUserInfo SET weixinuuid=:2,photo=:3,name=:4,qquuid=:5,weibouuid=:6,accountlogin=:7 WHERE devicesId=:1")
    void upLoginOutByDevicesId(String devicesId,String weixinuuid,String photo,String name,String qquuid,String weibouuid,String accountlogin);
	//微博登录
	@SQL("UPDATE AppUserInfo SET weibouuid=:2 WHERE devicesId=:1")
    void upwbByDevicesId(String devicesId,String weibouuid);
	//QQ登录
	@SQL("UPDATE AppUserInfo SET qquuid=:2 WHERE devicesId=:1")
    void upqqByDevicesId(String devicesId,String qquuid);
	//修改头像
	@SQL("UPDATE AppUserInfo SET photo=:2 WHERE uuid=:1")
    void upMyPhotoByUUID(String uuid,String photo);
	//修改昵称
	@SQL("UPDATE AppUserInfo SET name=:2 WHERE uuid=:1")
    void upMyNameByUUID(String uuid,String name);
	
	//修改头像
	@SQL("UPDATE AppDevicesAndUid SET photo=:2 WHERE uuid=:1")
    void upAppPhotoByUUID(String uuid,String photo);
	//修改昵称
	@SQL("UPDATE AppDevicesAndUid SET name=:2 WHERE uuid=:1")
    void upAppNameByUUID(String uuid,String name);
	
	//查询用户评论-------------------
	@SQL("SELECT id,newsId,newsTitle,reply,count(newsId) FROM Comments WHERE deleteFlag=1 AND replyName =:1 GROUP BY newsId ORDER BY stickAt DESC,createAt DESC LIMIT :2,:3 ")
    List<Map> queryCommentsByUserId(String replyName,int start,int length);
	//查询用户评论
	@SQL("SELECT count(1) FROM Comments WHERE deleteFlag=1 AND replyName =:1 ")
    int queryCommentsByUserIdSize(String replyName);
	//删除评论
	@SQL("UPDATE Comments SET deleteFlag=0 WHERE id=:1 ")
	void delCommentsById(int id);
	//查询用户收藏-------------------
	@SQL("select id,newsId,newsTitle from Collect where deleteFlag=1 AND userName =:1 ORDER BY updateAt DESC LIMIT :2,:3 ")
	List<Map> queryCollectById(String userName,int start,int length);
	//查询用户收藏
	@SQL("select count(1) from Collect where deleteFlag=1 AND userName =:1 ")
	int queryCollectByIdSize(String userName);
	//删除收藏
	@SQL("UPDATE Collect SET deleteFlag=0 WHERE id=:1 ")
	void delCollectById(int id);
	//查询我的问-------------------
	@SQL("select id,msg from Feedback where deleteFlag=1 and status=0 and userId=:1 ORDER BY stickAt DESC,updateAt DESC LIMIT :2,:3 ")
	List<Map> queryMyQueById(int userId,int start,int length);
	//查询我的问size
	@SQL("select count(1) from Feedback where deleteFlag=1 and status=0 and userId=:1 ")
	int queryMyQueByIdSize(int userId);
	//查询我提的问题，别人的回答
	@SQL("select id,feedbackId,reply,createAt,replyUserId from FeedbackReply where deleteFlag=1 and feedbackId=:1 ORDER BY stickAt DESC,updateAt DESC LIMIT :2,:3 ")
	List<Map> queryMyQueAnsById(int feedbackId,int start,int length);
	//查询我提的问题，别人的回答
	@SQL("select count(1) from FeedbackReply where deleteFlag=1 and feedbackId=:1 ")
	int queryMyQueAnsByIdSize(int feedbackId);
	//删除我的提问
	@SQL("UPDATE Feedback SET deleteFlag=0 WHERE id=:1 ")
	void delMyQueById(int id);
	//查询我的答-------------------
	@SQL("select id as aid,userId,feedbackId,contents,reply,createAt from FeedbackReply where deleteFlag=1 and replyUserId=:1 ORDER BY stickAt DESC,updateAt DESC LIMIT :2,:3 ")
	List<Map> queryMyAnsById(int replyUserId,int start,int length);
	//查询我的答
	@SQL("select count(1) from FeedbackReply where deleteFlag=1 and replyUserId=:1 ")
	int queryMyAnsByIdSize(int replyUserId);
	//删除我的答
	@SQL("UPDATE FeedbackReply SET deleteFlag=0 WHERE id=:1 ")
	void delMyAnsById(int id);
	//查询新闻互动数-------------------
	@SQL("select id,title,carouselImage,abstractInfor,releaseAt,pvNum,praiseNum,commentsNum,forwardNum,collectNum,type from News where deleteFlag=1 and id=:1 ")
	List<Map> queryNewsById(int id);
	
	//查询设备与App是否存在-------------------
	@SQL("SELECT count(1) FROM AppDevicesAndUid WHERE devicesId=:1 AND uuid=:2 ")
	int queryAppDevicesAndUid(String devicesId,String uuid);
	
	//初始化App用户与设备和第三方 ID的关联
	@SQL("INSERT INTO AppDevicesAndUid (devicesId,uuid,photo,name,type) VALUES (:1,:2,:3,:4,:5) ")
	void createAppDevicesAndUid(String devicesId,String uuid,String photo,String name,String type);
	
	//查询设备与App是否存在-------------------
	@SQL("SELECT count(1) FROM AppDevicesAndUid WHERE uuid=:1 ")
	int queryAppByAccountLogin(String uuid);
	
	//----------------------------------Token操作
	@SQL("INSERT INTO AppToken (phone,token,createAt,state) VALUES (:1,:2,now(),1) ")
	void createToken(String phone,String token);
	
	@SQL("SELECT count(1) FROM AppToken WHERE phone=:1 AND token=:2 And state=1")
	int queryToken(String phone,String token);
	
	@SQL("UPDATE AppToken SET state=0 WHERE phone=:1 ")
	void updateToken(String phone);
}
